//
//  BCOrderRessultCell.m
//  BangcaiBuy
//
//  Created by lsy on 16/6/20.
//  Copyright © 2016年 BCB. All rights reserved.
//

#import "BCOrderRessultCell.h"
#import "BCOrderResultDetailCell.h"

@interface BCOrderRessultCell  ()<UITableViewDelegate, UITableViewDataSource>
{
    UIView *headView;
    UIButton *check;
    UIImageView *styleImg;
    UILabel *styleName;
    UILabel *priceLab;
    UIImageView *arrow;
    UILabel *headLine;
    
    UITableView *stuffTable;
    
    UILabel *footLine;

    UIView *footView;
    UILabel *shopNameLabel;
    UILabel *shopNumlabel;
    
    
    
}

@end

@implementation BCOrderRessultCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView
{  
    self.contentView.backgroundColor = [UIColor colorWithRed:0.961  green:0.961  blue:0.961 alpha:1];
    headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 65)];
    
    check = [UIButton buttonWithType:UIButtonTypeCustom];
    [check setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateNormal];
    [check setImage:[UIImage imageNamed:@"未选择"] forState:UIControlStateSelected];
    check.layer.cornerRadius = 10;
    check.layer.masksToBounds = YES;
    
    
    styleImg = [[UIImageView alloc] init];
    styleImg.layer.cornerRadius = 20;
    styleImg.layer.masksToBounds = YES;
    
    
    styleName = [[UILabel alloc] init];
    styleName.font = [UIFont systemFontOfSize:18];
   
    
    priceLab = [[UILabel alloc] init];
    priceLab.textColor = [UIColor colorWithRed:0.999  green:0.302  blue:0 alpha:1];
    priceLab.font = [UIFont systemFontOfSize:16];
    
    arrow = [[UIImageView alloc] init];
    arrow.image = [UIImage imageNamed:@"下一步-灰"];
    
    headLine = [[UILabel alloc] init];
    headLine.backgroundColor = [UIColor colorWithRed:0.949  green:0.949  blue:0.949 alpha:1];
    
    [headView addSubview:check];
    [headView addSubview:styleImg];
    [headView addSubview:styleName];
    [headView addSubview:priceLab];
    [headView addSubview:arrow];
    [headView addSubview:headLine];
    
    
    footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    
    
    shopNameLabel = [[UILabel alloc] init];
    shopNameLabel.textAlignment = NSTextAlignmentLeft;
    shopNameLabel.font = [UIFont systemFontOfSize:14];
    shopNameLabel.textColor = [UIColor colorWithRed:0.541  green:0.541  blue:0.541 alpha:1];
    shopNumlabel = [[UILabel alloc] init];
    shopNumlabel.textAlignment = NSTextAlignmentRight;
    shopNumlabel.textColor = [UIColor colorWithRed:0.541  green:0.541  blue:0.541 alpha:1];
    shopNumlabel.font = [UIFont systemFontOfSize:14];

    footLine = [[UILabel alloc] init];
    footLine.backgroundColor = [UIColor colorWithRed:0.949  green:0.949  blue:0.949 alpha:1];
    
    [footView addSubview:shopNameLabel];
    [footView addSubview:shopNumlabel];
    [footView addSubview:footLine];
    
   

    stuffTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 28*4+65+40)];
    stuffTable.dataSource = self;
    stuffTable.delegate = self;
    [stuffTable registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    stuffTable.rowHeight = 28;
    stuffTable.scrollEnabled = NO;
    stuffTable.separatorStyle = UITableViewCellEditingStyleNone;
    [stuffTable registerClass:[BCOrderResultDetailCell class] forCellReuseIdentifier:@"reuse"];
    stuffTable.tableHeaderView = headView;
    stuffTable.tableFooterView = footView;
    stuffTable.userInteractionEnabled = NO;
        [self.contentView addSubview:stuffTable];
    
   }

- (void)setDataArr:(NSArray *)dataArr
{
    _dataArr = dataArr;
    __block CGFloat f = 0;
    [dataArr enumerateObjectsUsingBlock:^(ORDER * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        f += (obj.price.floatValue - .5) * obj.count.integerValue;
    }];
    self.totalPrice = f;
    [stuffTable reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BCOrderResultDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    ORDER *stuff = self.dataArr[indexPath.row];
    cell.name = stuff.name;
    cell.price = [NSString stringWithFormat:@"%.2f", (stuff.price.floatValue - .5)];
    cell.amount = [NSString stringWithFormat:@"%@", stuff.count];
    cell.totalPrice = [NSString stringWithFormat:@"%.2f",(stuff.price.floatValue - .5) * (stuff.count.integerValue)];
    return cell;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    switch (_style) {
        case 0:
        {
            styleImg.image = [UIImage imageNamed:@"禽蛋肉品"];
            styleName.text = @"禽蛋肉品";
            priceLab.text = [NSString stringWithFormat:@"%.2f", self.totalPrice];
            shopNameLabel.text = @"供货方: 大连长兴市场26B-703";
            shopNumlabel.text = @"23家参与竞标";
        }
            break;
        case 1:
        {
            styleImg.image = [UIImage imageNamed:@"蔬菜水果"];
            styleName.text = @"蔬菜水果";
            priceLab.text = [NSString stringWithFormat:@"%.2f", self.totalPrice];
            shopNameLabel.text = @"供货方: 大连长兴市场26B-703";
            shopNumlabel.text = @"23家参与竞标";

        }
            break;
            
            
        default:
            break;
    }

    [stuffTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.mas_equalTo(0);
        make.height.mas_equalTo(28*_dataArr.count+65+40);
        make.top.mas_equalTo(0);
        
    }];
    
    [check mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12*wx);
        make.centerY.mas_equalTo(0);
        make.height.and.width.mas_equalTo(20);
        
    }];
    [styleImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(44 *wx);
        make.centerY.mas_equalTo(0);
        make.height.and.width.mas_equalTo(40);
        
    }];
    [styleName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(styleImg.mas_right);
        make.centerY.mas_equalTo(0);
        make.top.and.bottom.mas_equalTo(0);
    }];
    
    [priceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-100);
        make.centerY.mas_equalTo(0);
        make.top.and.bottom.mas_equalTo(0);
        
    }];
    
    [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(16 );
        make.width.mas_equalTo(10);
        make.right.mas_equalTo(-12);
        make.centerY.mas_equalTo(0);
       }];

    [headLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(-12);
        make.height.mas_equalTo(1);
        make.bottom.mas_equalTo(0);
        
    }];
    
    [footLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(-12);
        make.height.mas_equalTo(1);
        make.top.mas_equalTo(0);
        
    }];
    
    [shopNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.bottom.mas_equalTo(0);
        make.left.mas_equalTo(12);
        
    }];
    
    [shopNumlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.bottom.mas_equalTo(0);
        make.right.mas_equalTo(-12);
        
    }];
    
    
}

@end
